package server;

/**
 * This class represents a single job - a series of visual manipulations to perform on a specified representation
 * @author yariv zur
 *
 */
public class Job {

	private String id, resource, body, inputRep, outputRep, status;
	private boolean ready = false;
	/**
	 * Constructor for the Job class
	 * @param jobName - the id of the job
	 */
	public Job(String jobName){
		this.id = jobName;
	}
	/**
	 * Setter for the Job's body - the content of the XML file uploaded by the client in String form
	 * @param doc - the Job content
	 */
	public void setJobBody(String doc){
		this.body = doc;
		this.setInputRepresentation();
		body = body.replaceAll("><", ">\r\n<");
	}
	/**
	 * Setter for the Job's resource
	 * @param id - the Job's resource
	 */
	public void setJobResource(String id){
		this.resource = id;
	}
	/**
	 * Getter for the Job Id
	 * @return the Job's Id
	 */
	public String getJobId(){
		return this.id;
	}
	/**
	 * Getter for the Job's resource
	 * @return resource
	 */
	public String getResource(){
		return this.resource;
	}
	/**
	 * Getter for the Job's InputRepresentation - the image the job will be performed on
	 * @return the input representation's id
	 */
	public String getInputRepresentation(){
		return this.inputRep;
	}
	/**
	 * Setter for the Job's InputRepresentation - the image the job will be performed on
	 */
	public void setInputRepresentation(){
		int index1 = this.body.indexOf("InputRepresentation");
		int index2 = this.body.indexOf('"', index1)+1;
		this.inputRep = body.substring(index2, this.body.indexOf('"', index2));
	}
	/**
	 * Getter for the Job's OutputRepresentation - the image the Job's result will be saved to
	 * @return the output representation id
	 */
	public String getOutputRepresentation(){
		return this.outputRep;
	}
	/**
	 * Setter for the Job's OutputRepresentation - the image where the Job's result will be saved
	 * @param rep - the representation id
	 */
	public void setOutputRepresentation(String rep){
		this.outputRep = rep;
		int index1 = this.body.indexOf("InputRepresentation");
		int index2 = this.body.indexOf('<', index1);
		String part1 = this.body.substring(0, index2);
		String part2 = this.body.substring(index2, body.length());
		this.body = part1+"<OutputRepresentation id=\""+outputRep+"\"/>\r\n"+part2;
	}
	/**
	 * Getter for the Job's body
	 * @return the String content of the Job in XML syntax
	 */
	public String getJobBody(){
		return this.body;
	}
	/**
	 * Setter for the Job's status
	 * @param s - the status of the job
	 */
	public void setStatus(String s){
		this.status = s;
	}
	/**
	 * Getter for the Job's status - "not submitted", "submitted", "finished", "InputRepresentation N/A"
	 * @return the Job's current status
	 */
	public String getStatus(){
		return this.status;
	}

	public boolean isReady(){
		return this.ready;
	}

	public void setReady(){
		this.ready = true;
	}
}
